package microcredit.gui.table.impl;

import java.awt.Color;

import javax.swing.ImageIcon;

import microcredit.gui.table.EnumBeanTableModel;
import microcredit.gui.util.FormatUtilities;
import microcredit.model.Member;
import microcredit.model.MemberGroup;
import microcredit.model.MemberToGroup;
import microcredit.model.util.ModelUtils;


enum MemberToGroupAssignmentColumn {
	COLUMN_MEMBER_NUMBER,
	COLUMN_MEMBER,
	COLUMN_MEMBER_POSITION,
	COLUMN_MEMBER_SINCE,
}
public class MemberToGroupAssignmentTableModel extends
		EnumBeanTableModel<MemberToGroup, MemberToGroupAssignmentColumn> {

	@Override
	protected Object getValue(MemberToGroup o, MemberToGroupAssignmentColumn column) {
		switch (column) {
		case COLUMN_MEMBER : return o.getMember().getFullName();
		case COLUMN_MEMBER_SINCE : return FormatUtilities.formatDate(o.getSince());
		case COLUMN_MEMBER_POSITION : return getPositionInGroup(o);
		case COLUMN_MEMBER_NUMBER : return Member.formatID(o.getMember().getId());
		}
		return null;
	}
	
	@Override
	protected int getWidth(MemberToGroupAssignmentColumn column) {
		switch (column) {
		case COLUMN_MEMBER : return 300;
		case COLUMN_MEMBER_SINCE : return 100;
		case COLUMN_MEMBER_POSITION : return 100;
		case COLUMN_MEMBER_NUMBER : return 50;
		}
		return -1;
	}
	
	private String getPositionInGroup(MemberToGroup o) {
		if (o.getMemberGroup() != null) {
			if (ModelUtils.equals(o.getMember(), o.getMemberGroup()
					.getSecretaryMember())) {
				return "Secretary";
			}
			if (ModelUtils.equals(o.getMember(), o.getMemberGroup()
					.getTresurerMember())) {
				return "Tresurer";
			}
			if (ModelUtils.equals(o.getMember(), o.getMemberGroup()
					.getChairmanMember())) {
				return "Chairman";
			}
		}
		return " - "; 
	}
	
	@Override
	protected MemberToGroupAssignmentColumn getSortColumn() {
		return MemberToGroupAssignmentColumn.COLUMN_MEMBER;
	}

	@Override
	protected boolean isBold(MemberToGroup o,
			MemberToGroupAssignmentColumn column) {
		if (o.getMemberGroup() != null && ModelUtils.equals(o.getMember(), o.getMemberGroup().getSecretaryMember())
				|| ModelUtils.equals(o.getMember(), o.getMemberGroup().getTresurerMember())
				|| ModelUtils.equals(o.getMember(), o.getMemberGroup().getChairmanMember())) {
			return true;
		}
		return super.isBold(o, column);
	}
	
	@Override
	protected Color getForeground(MemberToGroup o,
			MemberToGroupAssignmentColumn column) {
		if (o.getMemberGroup() != null && ModelUtils.equals(o.getMember(), o.getMemberGroup().getSecretaryMember())
				|| ModelUtils.equals(o.getMember(), o.getMemberGroup().getTresurerMember())
				|| ModelUtils.equals(o.getMember(), o.getMemberGroup().getChairmanMember())) {
			return Color.BLUE;
		}
		return super.getForeground(o, column);
	}
	
	@Override
	protected String getName(MemberToGroupAssignmentColumn column) {
		switch (column) {
		case COLUMN_MEMBER_NUMBER : return "No";
		case COLUMN_MEMBER : return "Member";
		case COLUMN_MEMBER_SINCE : return "Member since";
		case COLUMN_MEMBER_POSITION : return "Position";
		}
		return null;
	}
	

	@Override
	public ImageIcon getRowIcon() {
		return new ImageIcon(MemberTableModel.class.getResource("/microcredit/icons/x16/id_card.png"));
	}

}